package com.example.controller;

import com.example.common.Result;
import com.example.entity.Book;
import com.example.entity.Introduction;
import com.example.service.BookService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping("/selectAll")
    public Result selectAll(Book book){
        List<Book> bookslist=bookService.selectAll(book);
        return Result.success(bookslist);
    }

    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Book book){
        PageInfo<Book> bookPageInfo = bookService.selectPage(pageNum, pageSize,book);
        return Result.success(bookPageInfo);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Book book){
        bookService.add(book);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Book book){
        bookService.update(book);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        bookService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/selectRank")
    public Result selectRank(){
        return null;
        //TODO
    }

    @GetMapping("selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Book book = bookService.selectById(id);
        return Result.success(book);
    }

}
